package com.nsn.mib.trap.parser;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import edu.stanford.nlp.trees.TypedDependency;

public class AlarmParser {
	
	public static void main(String[] srgs)
	{
		
		try {
			System.setOut(new PrintStream("stdout.txt"));
			String path = "cisico.xml";
			SAXReader dr = new SAXReader();
		
			Document d = dr.read(new File(path));
			
			Element root = d.getRootElement();
			
			for(Iterator<Element> i = root.elementIterator(); i.hasNext(); ) 
			{
				
			      Element element = i.next();
			      String name = element.elementText("NAME");
			      String description = element.elementText("DESC");
			      
			      List<TypedDependency> src = parser.DPFromString(description);
				  TrainParser tp = new TrainParser(name,src);
				  String r1 = null; //tp.getAlarmTextByName().trim();
				  String r2 = null;//tp.getAlarmTextFromName().trim();
				  String r3 = tp.getAlarmTextByDescription();
 			      //Print log
			      printXML(name,r3,tp.getTree().trim());
			} 
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	public static void printXML(String name ,String result,String tree)
	{
		System.out.println("<TRAP>");
		System.out.println("<NAME>");
		System.out.println(name);
		System.out.println("</NAME");
		System.out.println("<RESULT>");
		System.out.println(result);
		System.out.println("</RESULT>");
		System.out.println("<TREE>");
		System.out.println(tree);
		System.out.println("</TREE>");
		System.out.println("/TRAP");
	}
	
	private static StanfordParser parser = StanfordParser.getInstance();
	public static String getAlarmText(String name,String descrition)
	{
		List<TypedDependency> src = parser.DPFromString(descrition);
	    TrainParser tp = new TrainParser(name,src);
	    
	    String result =  tp.getAlarmTextByName().trim();
	    
	    if(result.length() < 5)
	    {
	    	String descri = ParserUtil.preprocess(descrition);
		    descri = ParserUtil.deleteBlank(descri);
		    src = parser.DPFromString(descri);
		    TrainParser tp1 = new TrainParser(name,src);
	    	result = tp1.getAlarmTextByDescription();
	    }
		return result.trim();
	}

}
